package com.gbx.ximalayating.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.RadioGroup;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.fragment.ClassicalFragment;
import com.gbx.ximalayating.fragment.HottestFragment;
import com.gbx.ximalayating.fragment.NewestFragment;

import java.util.ArrayList;
import java.util.List;

public class HottestRecommendActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private List<Fragment> list;
    private HottestFragment hottestFragment;
    private NewestFragment newestFragment;
    private ClassicalFragment classicalFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hottest_recommend);
        Toolbar toolbar = (Toolbar) findViewById(R.id.hottestRec_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.btn_back_orange);
        actionBar.setTitle("热门推荐");

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.hottest_btn_container);
        radioGroup.setOnCheckedChangeListener(this);
        list=new ArrayList<>();
        hottestFragment=new HottestFragment();
        newestFragment=new NewestFragment();
        classicalFragment=new ClassicalFragment();
        list.add(hottestFragment);
        list.add(newestFragment);
        list.add(classicalFragment);
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            transaction.add(R.id.hottestRec_fragment_container,list.get(i));
            transaction.hide(list.get(i));
        }
        transaction.commit();
        showFragment(0);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.hotest_btn:
                showFragment(0);
                break;
            case R.id.newest_btn:
                showFragment(1);
                break;
            case R.id.classical_btn:
                showFragment(2);
                break;
        }
    }
    public void showFragment(int position){
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            if (position==i){
                transaction.show(list.get(i));
            }else {
                transaction.hide(list.get(i));
            }
        }
        transaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
